package com.softcloud.comingforyuan.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.softcloud.comingforyuan.R;
import com.softcloud.comingforyuan.model.ActivityCollector;
import com.softcloud.comingforyuan.model.BaseActivity;

import org.w3c.dom.Text;

import java.util.Calendar;

public class MainActivity extends BaseActivity {

    private int lastHour;
    private int lastMin;
    private int lastSec;

    private TextView titleText;

    private Button toKnowledge;
    private Button toExercise;
    private Button toMistake;
    private Button practiseMistake;

    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pref = PreferenceManager.getDefaultSharedPreferences(this);

        titleText = (TextView)findViewById(R.id.title_text);
        titleText.setText("主页");

        toKnowledge = (Button)findViewById(R.id.main_learn_knowledge);
        toExercise = (Button)findViewById(R.id.main_start_exercise);
        toMistake = (Button)findViewById(R.id.main_check_mistake);
        practiseMistake = (Button)findViewById(R.id.main_practise_mistake);

        toKnowledge.setOnClickListener(new toKnowledgeListener());
        toExercise.setOnClickListener(new toExerciseListener());
        toMistake.setOnClickListener(new toMistakeListener());
        practiseMistake.setOnClickListener(new practiseMistakeListener());
    }

    class toKnowledgeListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,KnowledgeActivity.class);
            startActivity(intent);
        }
    }
    class toExerciseListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            editor = pref.edit();
            editor.putBoolean("practiseMistake",false);
            editor.commit();
            Intent intent = new Intent(MainActivity.this,ExerciseActivity.class);
            startActivity(intent);
        }
    }
    class practiseMistakeListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            boolean hasMistake = pref.getBoolean("hasMistake",false);
            if(!hasMistake){
                Toast.makeText(MainActivity.this,"无错题需要练习",Toast.LENGTH_SHORT).show();
            }else{
                editor = pref.edit();
                editor.putBoolean("practiseMistake",true);
                editor.commit();
                Intent intent = new Intent(MainActivity.this,ExerciseActivity.class);
                startActivity(intent);
            }
        }
    }
    class toMistakeListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            boolean hasMistake = pref.getBoolean("hasMistake",false);
            if(!hasMistake){
                Toast.makeText(MainActivity.this,"暂无错题记录",Toast.LENGTH_SHORT).show();
            }else{
                Intent intent = new Intent(MainActivity.this,MistakeActivity.class);
                startActivity(intent);
            }
        }
    }


    @Override
    public void onBackPressed() {
        Calendar calendar = Calendar.getInstance();
        int currHour = calendar.get(Calendar.HOUR);
        int currMin = calendar.get(Calendar.MINUTE);
        int currSec = calendar.get(Calendar.SECOND);
        if(currHour == lastHour && currMin == lastMin && currSec-lastSec<=2){
            ActivityCollector.finishAll();
        }else{
            lastHour = currHour;
            lastMin = currMin;
            lastSec = currSec;
            Toast.makeText(this,"再次点击退出程序",Toast.LENGTH_SHORT).show();
        }
    }
}
